package com.zpark.it.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.zpark.it.mapper.CarouselMapper;
import com.zpark.it.pojo.Carousel;
import com.zpark.it.service.CarouselService;
import com.zpark.it.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.stream.Collectors;

/**
 * @Title: CarouselServiceImpl
 * @Author Charles
 * @Package com.zpark.it.service.impl
 * @Date 2023/7/21 11:32
 */
@Service
@Slf4j
public class CarouselServiceImpl implements CarouselService {

    @Autowired
    private CarouselMapper carouselMapper;
    /**
     * 查询优先级最高的六条数据
     *安装优先级查询数据库
     * 使用stream流，进行内存数据切割，保留6条数据
     * @return
     */
    @Override
    @Cacheable(value = "list.carousel",key = "#root.methodName",cacheManager = "cacheManagerDay")
    public R list() {
        QueryWrapper<Carousel>  carouselQueryWrapper=new QueryWrapper<>();
        carouselQueryWrapper.orderByDesc("priority");
        List<Carousel> list = carouselMapper.selectList(carouselQueryWrapper);
        //使用jdk1.8stream进行切割
        List<Carousel> collect = list.stream().limit(6).collect(Collectors.toList());
        R ok = R.ok(collect);
        log.info("结果为：{}","成功");
        return ok;
    }
}
